package com.cuijq.ms.Iteratordesign.v2;

import java.util.NoSuchElementException;

/**
 * @author by CuiJQ
 * @Classname ArrayIterator
 * @Description ArrayIterator Description
 * @Date 2021/5/29 16:53
 */
public class ArrayIterator<E> implements Iterator<E> {

  private int cursor;
  private ArrayList<E> arrayList;

  public ArrayIterator(ArrayList<E> arrayList) {
    this.cursor = 0;
    this.arrayList = arrayList;
  }

  @Override
  public boolean hasNext() {
    return cursor < arrayList.size();
  }

  @Override
  public E next() {
    cursor++;
    return (E) arrayList.get(cursor);
  }

  @Override
  public E currentItem() {
    if (cursor >= arrayList.size()) {
      throw new NoSuchElementException();
    }
    return (E) arrayList.get(cursor);
  }

}
